
var OrangeReady = function (doc, win, undefined) {
	// DomReady
	var domLoaded = function (callback) {

		/* Mozilla, Chrome, Opera */
		if (document.addEventListener) {
			document.addEventListener('DOMContentLoaded', callback, false);
		}
		/* Safari, iCab, Konqueror */
		if (/KHTML|WebKit|iCab/i.test(navigator.userAgent)) {
			var DOMLoadTimer = setInterval(function () {
				if (/loaded|complete/i.test(document.readyState)) {
					callback();
					clearInterval(DOMLoadTimer);
				}
			}, 10);
		}
		/* Other web browsers */
		window.onload = callback;
	};
	
	var orientationChanged = function(callback) {
		
		var orientation = "landscape";
		
		// Detect whether device supports orientationchange event, otherwise fall back to
		// the resize event.
		var supportsOrientationChange = "onorientationchange" in window,
			orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";

		win.addEventListener(orientationEvent, function () {
			if (window.orientation != orientation) {
				orientation = window.orientation;	

				setTimeout(callback, 200);
			}
		}, false);
	}

	var breadCrumbs = function() {
		
		var _me,
			//_orientation = 'landscape',
			_dom = {
				"container": doc.getElementById("breadcrumbs")
			};			
			
		return {
			
			//container: doc.getElementById("breadcrumbs"),		
			
			init: function() {
				_me = this;
				_me.fit();
				orientationChanged(_me.fit);
			},
			
			space: function () {

				return ( doc.width - search.container.offsetWidth - 80 );
			},
			
			width: function () {
				
				var totalWidth = 0;
				var lis = _dom.container.children;

				for (var i in lis) {
					if ( lis[i].offsetWidth > 0 ) {
						totalWidth += lis[i].offsetWidth;
					}
				}
				return totalWidth;
			},

			isShortened: function () {
				
				if (_dom.container.children[1].children[0].getAttribute("title"))
					return true;
				else return false;
			},
			
			shorten: function ( charsAmount ) {
				// If there are no space for the breadcrumbs shortens all the items, but the first and last ones			
				var lis = _dom.container.children;
				var last = lis.length-1;

				for (var i in lis) {
					if( i>0 && i<last) {
						var currentA = lis[i].children[0];
						var currentSpan = lis[i].children[0].children[0];
						
						var text = currentSpan.innerText;
						currentA.setAttribute("title", text);

						text = text.substring(0, charsAmount) + "...";
						currentSpan.innerText = text;
					}
				}
			},

			unShorten: function ( charsAmount ) {
				// If there are no space for the breadcrumbs shortens all the items, but the first and last ones			
				var lis = _dom.container.children;
				var last = lis.length-1;

				for (var i in lis) {
					if( i>0 && i<last) {
						var currentA = lis[i].children[0];
						var currentSpan = lis[i].children[0].children[0];
						
						var text = currentA.getAttribute("title");
						currentA.removeAttribute("title");
						currentSpan.innerText = text;
					}
				}
			},
			
			fit: function() {				

				if (_me.space() < _me.width()) {				
					if ( _me.isShortened() == false )
						_me.shorten(3);
				}
				else {
					if ( _me.isShortened() == true ) {
						_me.unShorten();
					}
				}
				
				
			}
		}
	}();
	
	
	var search = {

		container: doc.getElementById("search"),
		input: doc.getElementById("string"),
		button: doc.getElementById("button"),

		breadcrumbs: document.getElementById("breadcrumbs"),
		logo: document.getElementById("orange"),
		
		init: function() {
			
			if (this.container.offsetWidth <= 53) {					
				this.container.className = "closed";
			};

			this.button.addEventListener('click', orange.searchButtonClick);
			
			orientationChanged(this.fit);
		},

		open: function(event) {
			
			var searchClass = this.container.className;
			this.container.className = searchClass.replace(/\bclosed\b/,'opened');
			
			this.input.focus();

			this.breadcrumbs.className = "hide";
			this.logo.className = "hide";

		},
		
		close: function() {
		
			var searchClass = this.container.className;
			this.container.className = searchClass.replace(/\bopened\b/,'closed');

			this.breadcrumbs.className = "";
			this.logo.className = "";

		},
		
		submit: function() {
		
			var searchString = this.input.value;
            window.location = "category.aspx?type=4&src=" + searchString;			

		},

		fit: function() {

			if (search.container.offsetWidth <= 53) {	
				search.container.className = "closed";
			}
			else search.container.className="";
			search.input.blur();
		}

	};	
	
    var getHashValue = function() {

        var arr = window.location.hash.split("#");

        var hasValue = arr[1];

        //sets default
        if (typeof hasValue == "undefined") {
            return false;
        }

        var hashLen = hasValue.indexOf("?");
        if(hashLen>0){
            hasValue = hasValue.substring(0,hashLen);
        }
        return hasValue;

    };

	


	
	var orange = {}; // Public
		
	orange.searchButtonClick = function (event) {
			
		var searchClass = search.container.className;
		
		if ( searchClass !== "") {
		
			if ( searchClass === "closed" ) {
				search.open(event);	
			}
		
			else if ( (searchClass === "opened") && (search.input.value !== "") ) search.submit();
		
			else {
				search.close();
			}
		}
		
		else {
			
			if (search.input.value !== "") search.submit();
			else search.input.focus();
		}

	};

	var removeZoomStyles = function () {
		var head = doc.getElementsByTagName("head")[0],			
			zoomStyles = doc.getElementsByClassName("zoom-style"),
			numStyles = zoomStyles.length;

		//live collection! iterate from the end
		for (var i=numStyles-1; i>=0; i--) {
			head.removeChild(zoomStyles[i]);
		}
	};

	var initOranges = function () {
				
		window.addEventListener("hashchange", function() { 
			if (getHashValue() === false) search.close();
		}, false);

		search.init();
		
		breadCrumbs.init();
		
		// Hiding adress bar of Android's browser
		if (navigator.userAgent.match(/Android/i)){
			setTimeout(function () {
				window.scrollTo(0, 1);
			}, 1000);
		}

		if (window.innerWidth >= 480) {
			removeZoomStyles();
		}
		
	};

	//domLoaded(initOranges);
	initOranges();

	return orange;
}(document, window);